let path = require('path');

let HtmlWebpackPlugin = require('html-webpack-plugin');
let webpack = require('webpack');

// 模块 happypack 可以实现多线程来打包
// let Happypack = require('happypack');

module.exports = {
  devServer: {
    port: 3000,
    open: true,
    contentBase: './dist',
  },
  mode: 'production',
  entry: './src/index.js',
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist'),
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: './public/index.html',
      filename: 'index.html',
    }),
    // 从 moment 引入 ./locale 语言包时，将它忽略
    new webpack.IgnorePlugin(/\.\/locale/, /moment/),
    new webpack.DllReferencePlugin({
      manifest: path.resolve(__dirname, 'dist', 'manifest.json'),
    }),
    // new Happypack({
    //   id: 'js',
    //   use: [
    //     {
    //       loader: 'babel-loader',
    //       options: {
    //         presets: [
    //           '@babel/preset-env',
    //           '@babel/preset-react',
    //         ],
    //       }
    //     }
    //   ]
    // }),
    // new Happypack({
    //   id: 'css',
    //   use: ['style-loader', 'css-loader'],
    // }),
  ],
  module: {
    noParse: /jquery/, // 不去解析 jquery 中是否有依赖关系
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        include: path.resolve('./src'),
        // use: 'Happypack/loader?id=js'
        use: {
          loader: 'babel-loader',
          options: {
            presets: [
              '@babel/preset-env',
              '@babel/preset-react',
            ]
          }
        }
      },
      {
        test: /\.css$/,
        // use: 'Happypack/loader?id=css',
        use: ['style-loader', 'css-loader']
      }
    ]
  }
};
